package managedbean.FRM.CustomerInvoice;

import entity.CRM.Customer;
import entity.FRM.CustomerInvoice;
import java.io.IOException;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.CustomerBeanLocal;
import session.stateless.FRM.CustomerPaymentBeanLocal;

@ManagedBean(name="viewCustomerInvoicesPendingManagerBean")
@ViewScoped
public class ViewCustomerInvoicesPendingManagerBean implements Serializable {
    public ViewCustomerInvoicesPendingManagerBean() {}
    
    @EJB
    private CustomerPaymentBeanLocal customerPaymentBeanLocal;
    @EJB
    private CustomerBeanLocal customerBeanLocal;
    
    private Customer selectedCustomer;
    private CustomerInvoice selectedCustomerInvoice;
    private String statusMessage;
    private boolean isInvoiceExisting;
    private Double paymentAmount;
    
    public void initView(PhaseEvent event){
        try
        {
            if(selectedCustomer==null)
            {
                    selectedCustomer = (Customer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedCustomer");                          
         }            
        }
        catch (Exception ex)
        {
            ex.printStackTrace();            
        }
    }

    public boolean isIsInvoiceExisting() {
        return isInvoiceExisting;
    }

    public void setIsInvoiceExisting(boolean isInvoiceExisting) {
        this.isInvoiceExisting = isInvoiceExisting;
    }
    
    
    public CustomerInvoice getSelectedCustomerInvoice() {
        return selectedCustomerInvoice;
    }

    public void setSelectedCustomerInvoice(CustomerInvoice selectedCustomerInvoice) {
        this.selectedCustomerInvoice = selectedCustomerInvoice;
    }

    public Double getPaymentAmount() {
        return paymentAmount;
    }

    public void setPaymentAmount(Double paymentAmount) {
        this.paymentAmount = paymentAmount;
    }
    
    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }

    public void proceedPayment(ActionEvent event) throws IOException
    {
        if (selectedCustomerInvoice !=null)
        {
            this.setIsInvoiceExisting(true);
        }
    }
    
    public void makePayment(ActionEvent event) throws IOException
    {
        if (selectedCustomerInvoice !=null)
        {
//            System.out.println("test b4 makePayment" + paymentAmount);
//            System.out.println("test b5 makePayment" + selectedCustomerInvoice.getCustomerInvoiceAmount());

            if (selectedCustomerInvoice.getCashInAdvance() == null) 
            {
                boolean isValid = customerPaymentBeanLocal.isValid_checkPayment(selectedCustomerInvoice.getCustomerInvoiceId(), paymentAmount);
                if (isValid) {
                    customerPaymentBeanLocal.createCustomerPayment(selectedCustomerInvoice.getCustomerInvoiceId(), paymentAmount);
                    statusMessage = "New payment Saved Successfully";
                } else {
                    statusMessage = "Payment failed: Amount is not sufficient";
                }
            }
            else 
            {
                boolean isValid_CIA = customerPaymentBeanLocal.isValid_checkPaymentCIA(selectedCustomerInvoice.getCustomerInvoiceId(), paymentAmount);
                if (isValid_CIA) {
                    customerPaymentBeanLocal.createCustomerPayment_CIA(selectedCustomerInvoice.getCustomerInvoiceId(), paymentAmount);
                    statusMessage = "New payment Saved Successfully";
                } else {
                    statusMessage = "Payment failed: Amount is not sufficient";
                }
            }
        }
        else
            statusMessage= "Invalid: CustomerInvoice is not found";
        
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + statusMessage, ""));   
        this.setSelectedCustomer(customerBeanLocal.findCustomerById(this.getSelectedCustomer().getRegistrationNumber()));
    }
        
}
